﻿using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_a_simplified_nested_relation
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;

        public class GuiPerson { public GuiLayer Layer{ get; set; } }
        public class GuiLayer { public GuiAddress Address { get; set; } }
        public class GuiAddress { public String City { get; set; } }

        public class DomainPerson { public DomainLayer Layer { get; set; } }
        public class DomainLayer { public DomainAddress Address { get; set; } }
        public class DomainAddress { public String City { get; set; } }

        public When_mapping_a_simplified_nested_relation()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            mapping.Relate(domain => domain.Layer.Address.City, gui => gui.Layer.Address.City);
        }
        [Fact]
        public void Should_map_it()
        {
            var domainPerson = new DomainPerson { Layer = new DomainLayer { Address = new DomainAddress { City = "City From DomainPerson" } } };
            var guiPerson = mapping.Map(domainPerson, new GuiPerson { Layer = new GuiLayer { Address = new GuiAddress { City = "" } } });
            Assert.Equal("City From DomainPerson",guiPerson.Layer.Address.City);
        }

        [Fact]
        public void Should_throw_meaningful_exception_when_nested_toObjects_are_not_instanciated()
        {
            var domainPerson = new DomainPerson{Layer = new DomainLayer{Address = new DomainAddress{City = ""}}};
            Assert.Throws<GlueException>(() =>
                mapping.Map(domainPerson, new GuiPerson())
            );
            
            
        }
    }
}
